home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 9 / The PC-SIG Library on CD ROM - Ninth Edition.iso / 401_500 / DISK0416 / DISK0416.ZIP / ROFF45.C < prev    next >
C/C++ Source or Header  |  1985-08-08  |  9KB  |  310 lines

  1. /********************************************************/
  2. /*                                                      */
  3. /*                  ROFF4, Version 1.50                 */
  4. /*                                                      */
  5. /* (C) 1983 by  Ernest E. Bergmann                      */
  6. /*              Physics, Building #16                   */
  7. /*              Lehigh Univerisity                      */
  8. /*              Bethlehem, Pa. 18015                    */
  9. /*                                                      */
  10. /* Permission is hereby granted for all commercial and  */
  11. /* non-commercial reproduction and distribution of this */
  12. /* material provided this notice is included.           */
  13. /*                                                      */
  14. /********************************************************/
  15. /*June 27, 1983*/
  16. #include "roff4.h"
  17. /****************************************/
  18. puttl3(s1,s2,s3,pageno)
  19. char *s1,*s2,*s3;
  20. int pageno;             /*put out three part title, none
  21.                         containing '\n', with
  22.                         optional page numbering; aligning
  23.                         with page margins,0 & OWVAL;*/
  24. {
  25.       int size1,gap1,size2,gap2,size3,gaps,remain;
  26.       size1 =strln3(s1,FALSE,pageno);
  27.       OUTTOP=LTOP;
  28.       OUTBOT=LBOT;
  29.       size2 =strln3(s2,FALSE,pageno);
  30.       if(LTOP<OUTTOP) OUTTOP=LTOP;
  31.       size3 =strln3(s3,FALSE,pageno);
  32.       if(LTOP<OUTTOP) OUTTOP=LTOP;
  33.       gaps = max(0,OWVAL-size1-size2-size3);
  34.       gap1 = gaps/2;
  35.       gap2 = gaps-gap1;
  36.       remain=OWVAL;
  37.       if(size1<=remain){
  38.             puttl(s1,pageno);
  39.             remain-=size1;
  40.       }
  41.       if(gap1<remain){
  42.             blanks(gap1);
  43.             remain-=gap1;
  44.       }
  45.       if(size2<=remain){
  46.             puttl(s2,pageno);
  47.             remain-=size2;
  48.       }
  49.       if(gap2<remain){
  50.             blanks(gap2);
  51.             remain-=gap2;
  52.       }
  53.       if(size3<=remain){
  54.             puttl(s3,pageno);
  55.             remain-=size3;
  56.       }
  57.       printout();
  58.       putchar('\r');
  59. }
  60. /****************************************/
  61. blanks(i)
  62. int i;  /*sends i blanks to OUTBUF2*/
  63. {       
  64.       if(i<0) return;
  65.       for( ; i ; i--) putout( BLANK );
  66. }
  67. /****************************************/
  68. gettl3(sl,ttl1,ttl2,ttl3)
  69. char *sl,ttl1[MAXLINE],**ttl2,**ttl3;
  70. /* Gets from source line three part title that it
  71.         transfers to buffer delineated by ttl1 which has
  72.         capacity for all three strings; none of the strings
  73.         will contain '\n' */
  74. {
  75.       char c, cc, *dp;
  76. #ifdef DEBUGON
  77.       if DEBUG fprintf(STDERR,"\n\nGETTL3 sl =<%s>",sl);
  78. #endif
  79.       /*pass over command*/
  80.       for(c=*sl;c!=' '&&c!='\n'&&c!='\t';sl++)c=*sl;
  81.       /*advance to first non-blank or '\n' */
  82.       for(;c==' '||c=='\t';sl++)c=*sl;
  83.       /*advance beyond delim. if present:*/
  84.       if(c!='\n'&&!c) sl++;
  85.       dp=ttl1;
  86.       transfer(&sl,&dp,c);
  87.       *ttl2=dp;
  88.       transfer(&sl,&dp,c);
  89.       *ttl3=dp;
  90.       transfer(&sl,&dp,c);
  91. #ifdef DEBUGON
  92.       if DEBUG
  93.           fprintf(STDERR,"\ndelim=<%c>\nT1=<%s>\nT2=<%s>\nT3=<%s>;\n",
  94.       c,ttl1,*ttl2,*ttl3);
  95. #endif
  96. }
  97. /****************************************/
  98. transfer(s,d,c)
  99. char c;         /*terminal character*/
  100. char **s;       /*source string*/
  101. char **d;       /*destination string*/
  102. /* Copy string from source to destination.  Original delim.
  103. can be \0, \n, or char.  The pointer to the source is updated
  104. to point at the \0, \n, or past char.  In destination, delim.
  105. is always replaced by \0.  The destination pointer always
  106. points past this \0. */
  107. {
  108.       char a;
  109.       a=**s;
  110.       while(a!=c && a!='\n' && a)
  111.       {
  112.             **d = a;  
  113.             (*d)++;
  114.             (*s)++;  
  115.             a=**s;
  116.       }
  117.       **d='\0';
  118.       (*d)++;
  119.       if(a!='\n' && a!='\0') (*s)++;
  120. }
  121. /**********************************************************
  122.                 centers a line of text
  123. **********************************************************/
  124. center (line)
  125. char *line;
  126. {
  127.       TIVAL = max(( RMVAL+TIVAL-strln3(line,FALSE,1))/2, 0 );
  128.       OUTTOP=LTOP;
  129.       OUTBOT=LBOT;
  130.       return;
  131. }
  132. /************************************************************
  133. Revised April 24,83.Transfers next word from in to out.  Scans
  134. off leading white space from in.  If there is no word, returns
  135. FALSE.  Otherwise, input is truncated on left of word which is
  136. transfered to out without leading or trailling blanks.
  137. WE_HAVE_A_WORD will be returned.  If the transfered word
  138. terminates a sentence then SENTENCE is set to 1, otherwise it
  139. is reset to FALSE.
  140. **************************************************************/
  141. int getwrd (in,  out )
  142. char *in, *out;
  143. {
  144.       char *pin,*pout,c,cm,cp;
  145.       skip_blanks(in);
  146.       replace_char(in,TAB,BLANK);
  147.       pin=in;
  148.       pout=out;
  149.       c=*pin;
  150.       SENTENCE=FALSE;
  151.       if(c==NEWLINE || c=='\0')
  152.       {
  153.             *out='\0';
  154. #ifdef DEBUGON
  155.             if DEBUG fprintf(STDERR,"\ngetwrd=<%s>",out);
  156. #endif
  157.             return(FALSE);
  158.       }
  159.       while(c!=BLANK && c!=NEWLINE && c)
  160.       {
  161.             *(pout++)=c;
  162.             *pin=BLANK;
  163.             c=*(++pin);
  164.       }
  165.       *pout='\0';     /*terminate out string*/
  166.       cm=*(pout-1);
  167.       cp=*(pin+1);
  168.       switch (cm)
  169.       {
  170.       case ':' :
  171.       case ';' :
  172.       case '?' :
  173.       case '!' :
  174.             SENTENCE=1;
  175.             break;
  176.       case '.' :
  177.             if(cp==BLANK||cp==NEWLINE||c==NEWLINE)
  178.                   SENTENCE=1;
  179.       }
  180. #ifdef DEBUGON
  181.       if DEBUG fprintf("\ngetwrd=<%s>",out);
  182. #endif
  183.       return(WE_HAVE_A_WORD);
  184. }
  185. /*******************************************************
  186. Truncates white-space characters at the end of a string.
  187. ********************************************************/
  188. trunc_bl (string)
  189. char *string;
  190. {
  191.       char *ptr;
  192.       int k;
  193.       k = strlen (string);
  194.       ptr = &string[ k-1 ];   /* char before terminating nul */
  195.       while (*ptr==BLANK || *ptr==TAB || *ptr==NEWLINE)       
  196.             *ptr--  = '\0';
  197. }
  198. /*********************************************
  199.         distribute words evenly across a line
  200. **********************************************/
  201. spread ( line, nextra, no_words)
  202. char *line;
  203. int nextra;     /* no. extra places left in line */
  204. int no_words;   /* no. words in the line         */
  205. {
  206.       int i, j, nblanks, nholes;
  207. #ifdef DEBUGON
  208.       if DEBUG fprintf(STDERR,"spread:line=<%s>,\nnextra=%d, no_words=%d\n",
  209.       line,      nextra,    no_words   );
  210. #endif
  211.       if (nextra <= 0 || no_words <= 1)       return;
  212.       DIR = !(DIR);
  213.       nholes = no_words - 1;
  214.       trunc_bl (line);
  215.       i = strlen(line) - 1 ; /* last character of string */
  216.       j = min(MAXLINE-2,i+nextra);    /* last  position in output */
  217.       line[j+1] = '\0';
  218.       for ( ; i<j ; i--, j-- )
  219.       { 
  220.             line[j] = line[i];
  221.             if ( line[i] == BLANK)
  222.             { 
  223.                   if (DIR == 0) nblanks=(nextra-1)/nholes+1;
  224.                   else            nblanks = nextra/nholes;
  225.                   nextra = nextra - nblanks;
  226.                   nholes = nholes - 1;
  227.                   for ( ; nblanks > 0;  nblanks-- )
  228.                         line[--j] = BLANK;
  229.             }
  230.       }
  231. }
  232. /************************************************
  233. place portion of title line with optional page no. in OUTBUF2
  234. *************************************************/
  235. puttl ( str, num )
  236. char *str;
  237. int num;
  238. {
  239.       int i;
  240.       char c;
  241.       for (i=0;c= *str; str++)
  242.       {
  243.             if ( c != NUMSIGN ) putout(c);
  244.             else putnum(num);
  245.       }
  246. }
  247. /*******************************************
  248.         put out num to OUTBUF2 (conversion)
  249. ********************************************/
  250. putnum ( num, w )
  251. int num;
  252. {
  253.       int i, nd;
  254.       char chars[10];
  255.       nd = itoc ( num, chars, 10 );
  256.       for ( i=0;i<nd; i++) putout(chars[i]);
  257. }
  258. /************************************************
  259.         convert int num to char string in numstr
  260. *************************************************/
  261. itoc ( num, numstr, size )
  262. int num;
  263. char *numstr;
  264. int size;       /* largest size of numstr */
  265. {
  266.       int absnum, i, j, k, d;
  267.       absnum = abs (num);
  268.       numstr[0] = '\0';
  269.       i = 0;
  270.       do      { 
  271.             i++;
  272.             d = absnum % 10;
  273.             numstr[i] = d + '0';
  274.             absnum = absnum/10;
  275.       }
  276.       while ( absnum != 0 && i<size );
  277.       if ( num < 0 && i<size )
  278.       { 
  279.             i++;
  280.             numstr[i] = '-';
  281.       }
  282.       for( j=0; j<i; j++ )
  283.       { 
  284.             k = numstr[i];
  285.             numstr[i] = numstr[j];
  286.             numstr[j] = k;
  287.             i--;
  288.       }
  289.       return ( strlen(numstr) );
  290. }
  291. /****************************************/
  292. putout(c)       /*places c in OUTBUF2[]*/
  293. char c;
  294. {
  295.       if(c==SCVAL) c= BLANK;
  296.       if(c==NEWLINE)c='\0';
  297.       OUTBUF2[BPOS++]=c;
  298.       OUTBUF2[BPOS]='\0';/*safty net*/
  299. }
  300. /************************************
  301.         replace c1 in string with c2
  302. *************************************/
  303. replace_char (string, c1, c2)
  304. char *string, c1, c2;
  305. {
  306.       int i;
  307.       for (i=0; string[i]; i++)
  308.             if (string[i] == c1)    string[i] = c2;
  309. }
  310.